/*
 * Copyright (c) 2015 Strong Group - 版权所有
 * 
 * This software is the confidential and proprietary information of
 * Strong Group. You shall not disclose such confidential information 
 * and shall use it only in accordance with the terms of the license 
 * agreement you entered into with www.cnstrong.cn.
 */
package com.zuorenke.encrypt;

import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 *
 * 描述:
 */
public class RSA {

	public static final String ALGORITHMS = "RSA";

	@SuppressWarnings("unused")
	private static void generateKey() {
		try {
			KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHMS);
			kpg.initialize(1024);
			KeyPair kp = kpg.genKeyPair();
			PublicKey publicKey = kp.getPublic();
			PrivateKey privateKey = kp.getPrivate();

			String publicString = Base64.encodeBytes(publicKey.getEncoded());
			String privateString = Base64.encodeBytes(privateKey.getEncoded());

			System.out.println("公钥=" + publicString);
			System.out.println("私钥=" + privateString);
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	private static PrivateKey getPrivateKey(String privateKey) throws Exception {

		byte[] keyBytes = Base64.decode(privateKey);

		KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHMS);

		PrivateKey key = keyFactory.generatePrivate(keySpec);

		return key;
	}

	private static PublicKey getPublicKey(String publicKey) throws Exception {

		byte[] keyBytes = Base64.decode(publicKey);

		KeySpec keySpec = new X509EncodedKeySpec(keyBytes);

		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHMS);

		PublicKey key = keyFactory.generatePublic(keySpec);

		return key;
	}

	public static String encrypt(String content, String publicKey) throws Exception {
		// 获取公钥及参数e,n  
		RSAPublicKey pbk = (RSAPublicKey) getPublicKey(publicKey);
		BigInteger e = pbk.getPublicExponent();
		BigInteger n = pbk.getModulus();
		System.out.println("e= " + e);
		System.out.println("n= " + n);
		// 获取明文m  
		byte ptext[] = content.getBytes("UTF-8");
		BigInteger m = new BigInteger(ptext);
		// 计算密文c  
		BigInteger c = m.modPow(e, n);
		System.out.println("c= " + c);
		// 保存密文  
		String cs = c.toString();
		return cs;
	}

	public static String decrypt(String content, String privateKey) throws Exception {
		// 读取密文  
		BigInteger c = new BigInteger(content);
		// 读取私钥  
		RSAPrivateKey prk = (RSAPrivateKey) getPrivateKey(privateKey);
		BigInteger d = prk.getPrivateExponent();
		// 获取私钥参数及解密  
		BigInteger n = prk.getModulus();
		System.out.println("d= " + d);
		System.out.println("n= " + n);
		BigInteger m = c.modPow(d, n);
		// 显示解密结果  
		System.out.println("m= " + m);
		byte[] mt = m.toByteArray();
		return new String(mt,"UTF-8");
	}

	public static void main(String args[]) {
		String pk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDETCW8aKJsQTJYNnvCkfmNaONmdmV3Z0E2Pgo0g20yMmMIy29FXS5l5eMDzgL7WLK78I9fdZSoK/pyPoenzJ+DV5plW4gxjKSR8et6jGf/JIFhRey+NiabvPx/sKxnYTNBtA7lfl5xrYqcZRIficqxhCnlMj6qksnRmudtLDv19QIDAQAB";
		String rk = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMRMJbxoomxBMlg2e8KR+Y1o42Z2ZXdnQTY+CjSDbTIyYwjLb0VdLmXl4wPOAvtYsrvwj191lKgr+nI+h6fMn4NXmmVbiDGMpJHx63qMZ/8kgWFF7L42Jpu8/H+wrGdhM0G0DuV+XnGtipxlEh+JyrGEKeUyPqqSydGa520sO/X1AgMBAAECgYB6M6l+XGlZGVFdJzXuj++agASpAeXy6n3nZcH2pSbyi/mpPPRdaz/6QZ4M/VBP5w0GASFQXSOZeh9kxdavuKxIAbD05T1YVFSm21yurtfDLVd1vWISjcjbyNTP0S+UTiVFz3elQqouxMbv+C1CbNRK+wNVhToGcz1nKzW0/FvZFQJBAOUeUcwqg5dU2j8l3qKKWLKiSQaIAwkqfT/RP0oIEaMZKSJ9yP9+lpCbcCwTm1Lp/pvZdhGR8otLdV1c0TkHwl8CQQDbVAi6jY04FiPcY2w92w1mGjugCxXe4A++1F5WXoa2N6MqPtCFIKObi9iuWQmJsUhMjMsComr7I8Ofyniy07ArAkBuhszQ3+VxGGbcNamL4Y9eRTlTJEPKpSucoJWWYgQYGOgmEmXnEqsRgVqAeMjiG61Og34Yhb5qgfFRI5humZajAkAWLL7dLaaTfxmNFLhC4LzQD+LrYXOKnxi4cHxBOzlffuyOPt+SNiSUe2Ui63mdZEEtDCcSUx2B2rHSnAPsAJ8VAkAwL0+gM9pIpfwmZ9BT/PtxL3KOoSvWEcY9ee8/4mh5CpMwLEf8Mwlz5PnoL5wlKtliVhptVabDAwqzyBOKcGMV";
		try {
			String content = "279169831@qq.com&201506162130魏巧宝";
			String ec = encrypt(content, pk);
			System.out.println("RSA密文=" + ec);
			
			String ec2 = Des.encrypt(content);
			System.out.println("DES密文=" + ec2);
			
			String sc = decrypt(ec, rk);
			System.out.println("RSA原文="+sc);
			
			String sc2 = Des.decrypt(ec2);
			System.out.println("DES原文="+sc2);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
